package com.aspsine.multithreaddownload.slice;

import com.aspsine.multithreaddownload.*;
import com.aspsine.multithreaddownload.demo.util.ShowToast;
import com.aspsine.multithreaddownload.demo.util.Utils;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.media.image.common.Size;


import java.io.*;

public class DownloadAbilitySlice extends AbilitySlice{

    private Object name;

    private Text tvStatue;

    private ProgressBar progressBar;

    private String mUri;

    private Button buttonDownload;

    private Image mImage;

    public final int DOWNLOAD_START = 0;

    private int state = DOWNLOAD_START;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_download_main_demo);
        name = intent.getStringParam("name");
        mUri = intent.getStringParam("url");
        initButton();
    }

    @Override
    protected void onActive() {
        super.onActive();
    }

    @Override
    protected void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    private void initButton() {
        tvStatue = (Text)findComponentById(ResourceTable.Id_tvStatus);
        progressBar = (ProgressBar)findComponentById(ResourceTable.Id_progressBar);
        buttonDownload = (Button)findComponentById(ResourceTable.Id_btnDownload);
        buttonDownload.setClickedListener(this::download);
        mImage = (Image)findComponentById(ResourceTable.Id_icon);
        mImage.setPixelMap(null);
        tvStatue.setText("");
        buttonDownload.setText("Open");
        Text title = (Text)findComponentById(ResourceTable.Id_title);
        title.setText(""+name);
    }

    private void download(Component component){
        showPicture(component);
    }

    private void showPicture(Component component){
        try {
            File apk = new File(Utils.mDownloadDir,name+".jpg");
            if(apk.exists() && apk.isFile()){
                tvStatue.setText("Successful");
                buttonDownload.setText("Open");
                ImageSource.SourceOptions srcOpts = new ImageSource.SourceOptions();
                ImageSource imageSource = ImageSource.create(new FileInputStream(apk), srcOpts);
                ImageSource.DecodingOptions options = new ImageSource.DecodingOptions();
                options.desiredSize = new Size(320,320);
                PixelMap pixelMap = imageSource.createPixelmap(options);
                Image image = (Image)findComponentById(ResourceTable.Id_icon);
                image.setPixelMap(pixelMap);

            }else {
                //buttonDownload.setText("To Download");
                tvStatue.setText("Not Download");
               // ShowToast.showToast("Download faile !!!");
            }

        } catch (Exception e) {
            ShowToast.showToast("文件格式打不开");
        }
    }





}
